package com.fand.platform.configuration.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.configuration.dto.DictionaryTyDto;
import com.fand.platform.configuration.dto.DictionaryDto;
import com.fand.platform.configuration.dto.DictionaryLaDto;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.platform.configuration.validator.DictionaryTyValidator;
import com.fand.platform.configuration.validator.DictionaryValidator;
import com.fand.platform.configuration.validator.DictionaryLaValidator;
import com.fand.platform.configuration.vo.DictionaryVo;
import com.fand.platform.configuration.service.ConfigurationService;
import com.google.common.collect.Maps;

/**
 * 字典
 *
 * @author yaoHT
 */
@Controller
public class DictionaryController {
	@Qualifier(ConfigurationService.BEAN_ID)
	@Autowired(required = false)
	private ConfigurationService configurationService;

	@Qualifier(DictionaryTyValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator dictionaryTyValidator;

	@Qualifier(DictionaryValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator dictionaryValidator;

	@Qualifier(DictionaryLaValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator dictionaryLaValidator;

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminDictionaryTyDelete(final DictionaryVo dictionaryVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.configurationService.getDictionaryService().deleteDictionaryTyByIds(dictionaryVo.getDictionaryTy());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy/edit.html" }, method = RequestMethod.GET)
	public String adminDictionaryTyEdit(final DictionaryVo dictionaryVo, final Model model) {
		final DictionaryTyDto dictionaryTy = this.configurationService.getDictionaryService().
				getDictionaryTyById(dictionaryVo.getDictionaryTy());

		model.addAttribute("dictionaryTy", dictionaryTy);

		return "/admin/configuration/dictionary/dictionaryTy/edit";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy.html", "/admin/configuration/dictionary/dictionaryTy/index.html" })
	public String adminDictionaryTyIndex() {
		return "/admin/configuration/dictionary/dictionaryTy/index";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminDictionaryTyIndexPagination(final DictionaryVo dictionaryVo) {
		final PaginationDto<Map<String, Object>> dictionaryTyPagination = this.configurationService.getDictionaryService()
				.paginationDictionaryTyBy1ForMapList(dictionaryVo.getDictionaryTy());

		final Map<String, Object> dictionaryTyPaginationMap = Maps.newHashMap();

		dictionaryTyPaginationMap.put("total", dictionaryTyPagination.getTotalSize());
		dictionaryTyPaginationMap.put("rows", dictionaryTyPagination.getDataList());

		return dictionaryTyPaginationMap;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy/new.html" }, method = RequestMethod.GET)
	public String adminDictionaryTyNew(final DictionaryVo dictionaryVo, final Model model) {
		model.addAttribute("dictionaryTy", dictionaryVo.getDictionaryTy());

		return "/admin/configuration/dictionary/dictionaryTy/new";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminDictionaryTySave(final DictionaryVo dictionaryVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.dictionaryTyValidator.validate(dictionaryVo.getDictionaryTy(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.configurationService.getDictionaryService().saveDictionaryTyByIdOrCodeForDictionaryTy(dictionaryVo.getDictionaryTy());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy/validationBy1.json" })
	public @ResponseBody
	Boolean adminDictionaryTyValidationBy1(final DictionaryVo dictionaryVo) {
		try {
			final int count = this.configurationService.getDictionaryService().readDictionaryTyByCodeNqIdForInteger(
					dictionaryVo.getDictionaryTy());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryTy/view.html" }, method = RequestMethod.GET)
	public String adminDictionaryTyView(final DictionaryVo dictionaryVo, final Model model) {
		final DictionaryTyDto dictionaryTy = this.configurationService.getDictionaryService().
					getDictionaryTyById(dictionaryVo.getDictionaryTy());

		model.addAttribute("dictionaryTy", dictionaryTy);

		return "/admin/configuration/dictionary/dictionaryTy/view";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminDictionaryDelete(final DictionaryVo dictionaryVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.configurationService.getDictionaryService().deleteDictionaryByIds(dictionaryVo.getDictionary());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary/edit.html" }, method = RequestMethod.GET)
	public String adminDictionaryEdit(final DictionaryVo dictionaryVo, final Model model) {
		final DictionaryDto dictionary = this.configurationService.getDictionaryService().
				getDictionaryById(dictionaryVo.getDictionary());

		model.addAttribute("dictionary", dictionary);

		return "/admin/configuration/dictionary/dictionary/edit";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary.html", "/admin/configuration/dictionary/dictionary/index.html" })
	public String adminDictionaryIndex() {
		return "/admin/configuration/dictionary/dictionary/index";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminDictionaryIndexPagination(final DictionaryVo dictionaryVo) {
		final PaginationDto<Map<String, Object>> dictionaryPagination = this.configurationService.getDictionaryService()
				.paginationDictionaryBy1ForMapList(dictionaryVo.getDictionary());

		final Map<String, Object> dictionaryPaginationMap = Maps.newHashMap();

		dictionaryPaginationMap.put("total", dictionaryPagination.getTotalSize());
		dictionaryPaginationMap.put("rows", dictionaryPagination.getDataList());

		return dictionaryPaginationMap;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary/new.html" }, method = RequestMethod.GET)
	public String adminDictionaryNew(final DictionaryVo dictionaryVo, final Model model) {
		model.addAttribute("dictionary", dictionaryVo.getDictionary());

		return "/admin/configuration/dictionary/dictionary/new";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminDictionarySave(final DictionaryVo dictionaryVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.dictionaryValidator.validate(dictionaryVo.getDictionary(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.configurationService.getDictionaryService().saveDictionaryByIdOrCodeForDictionary(dictionaryVo.getDictionary());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary/validationBy1.json" })
	public @ResponseBody
	Boolean adminDictionaryValidationBy1(final DictionaryVo dictionaryVo) {
		try {
			final int count = this.configurationService.getDictionaryService().readDictionaryByCodeNqIdForInteger(
					dictionaryVo.getDictionary());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionary/view.html" }, method = RequestMethod.GET)
	public String adminDictionaryView(final DictionaryVo dictionaryVo, final Model model) {
		final DictionaryDto dictionary = this.configurationService.getDictionaryService().
					getDictionaryById(dictionaryVo.getDictionary());

		model.addAttribute("dictionary", dictionary);

		return "/admin/configuration/dictionary/dictionary/view";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminDictionaryLaDelete(final DictionaryVo dictionaryVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.configurationService.getDictionaryService().deleteDictionaryLaByIds(dictionaryVo.getDictionaryLa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa/edit.html" }, method = RequestMethod.GET)
	public String adminDictionaryLaEdit(final DictionaryVo dictionaryVo, final Model model) {
		final DictionaryLaDto dictionaryLa = this.configurationService.getDictionaryService().
				getDictionaryLaById(dictionaryVo.getDictionaryLa());

		model.addAttribute("dictionaryLa", dictionaryLa);

		return "/admin/configuration/dictionary/dictionaryLa/edit";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa.html", "/admin/configuration/dictionary/dictionaryLa/index.html" })
	public String adminDictionaryLaIndex() {
		return "/admin/configuration/dictionary/dictionaryLa/index";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminDictionaryLaIndexPagination(final DictionaryVo dictionaryVo) {
		final PaginationDto<Map<String, Object>> dictionaryLaPagination = this.configurationService.getDictionaryService()
				.paginationDictionaryLaBy1ForMapList(dictionaryVo.getDictionaryLa());

		final Map<String, Object> dictionaryLaPaginationMap = Maps.newHashMap();

		dictionaryLaPaginationMap.put("total", dictionaryLaPagination.getTotalSize());
		dictionaryLaPaginationMap.put("rows", dictionaryLaPagination.getDataList());

		return dictionaryLaPaginationMap;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa/new.html" }, method = RequestMethod.GET)
	public String adminDictionaryLaNew(final DictionaryVo dictionaryVo, final Model model) {
		model.addAttribute("dictionaryLa", dictionaryVo.getDictionaryLa());

		return "/admin/configuration/dictionary/dictionaryLa/new";
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminDictionaryLaSave(final DictionaryVo dictionaryVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.dictionaryLaValidator.validate(dictionaryVo.getDictionaryLa(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.configurationService.getDictionaryService().saveDictionaryLaByIdOrCodeForDictionaryLa(dictionaryVo.getDictionaryLa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa/validationBy1.json" })
	public @ResponseBody
	Boolean adminDictionaryLaValidationBy1(final DictionaryVo dictionaryVo) {
		try {
			final int count = this.configurationService.getDictionaryService().readDictionaryLaByCodeNqIdForInteger(
					dictionaryVo.getDictionaryLa());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/configuration/dictionary/dictionaryLa/view.html" }, method = RequestMethod.GET)
	public String adminDictionaryLaView(final DictionaryVo dictionaryVo, final Model model) {
		final DictionaryLaDto dictionaryLa = this.configurationService.getDictionaryService().
					getDictionaryLaById(dictionaryVo.getDictionaryLa());

		model.addAttribute("dictionaryLa", dictionaryLa);

		return "/admin/configuration/dictionary/dictionaryLa/view";
	}
}
